package com.snatch.snatchmgr.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.utils.StringUtils;
import com.snatch.common.vo.NodeWeb;
import com.snatch.common.vo.WebConfig;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.entity.db.ThirdMatch;
import com.snatch.snatchmgr.vo.GameType;
import com.snatch.snatchmgr.vo.SnatchConfig;
import com.snatch.snatchmgr.vo.SnatchGame;
import com.snatch.snatchmgr.vo.SnatchItem;
import com.snatch.snatchmgr.vo.SnatchWeb;
import com.snatch.snatchmgr.vo.SystemConfig;
import com.snatch.snatchmgr.vo.WebSnatchInfo;



/**
 * 系统在内数据接口
 * 
 * @author Administrator
 * 
 */
public class SystemData {
	/**
	 * 采集项
	 */
	private static SnatchConfig snatchConfig;
	
	/**
	 * 系统配置数据
	 */
	public static SystemConfig systemConfig;
	
	/**
	 * 第三方死盘 K: 第三方系统 “fb_hg” 皇冠_足球,”sb” 沙巴,”lj” 利记 V: 所有联赛数据 K: 赛事selfID+dataType V:
	 * 联赛对象( ThirdLeague )
	 */
	public static Map<String, ConcurrentHashMap<String, ThirdMatch>> gqThirdDatas = new ConcurrentHashMap<String, ConcurrentHashMap<String, ThirdMatch>>();
	
	/**
	 * 第三方死盘 K: 第三方系统 “fb_hg” 皇冠_足球,”sb” 沙巴,”lj” 利记 V: 所有联赛数据 K: 赛事selfID+dataType V:
	 * 联赛对象( ThirdLeague )
	 */
	public static Map<String, ConcurrentHashMap<String, ThirdMatch>> spThirdDatas = new ConcurrentHashMap<String, ConcurrentHashMap<String, ThirdMatch>>();
	
	/**
	 * 第三方系统 “fb_hg_第三方赛事id”
	 */
	public static Map<String,ConcurrentHashMap<String, ThirdLive>> liveMatchs = new ConcurrentHashMap<String,ConcurrentHashMap<String, ThirdLive>>();
	 /**
	  * 版本号
	  */
	public static Map<String,Long> versionMap = new ConcurrentHashMap<String,Long>();
	
	/**
	 * db操作记录
	 */
	public static Map<String, Long> serverCounter = new ConcurrentHashMap<String, Long>();
	
	/**
	 * key webCode 提供采集服务点
	 */
	public static ConcurrentHashMap<String, CopyOnWriteArrayList<WebSnatchInfo>> webSnachInfos = new ConcurrentHashMap<String, CopyOnWriteArrayList<WebSnatchInfo>>();
	
	
	public static Long findVersionAdd(String key){
		if(!versionMap.containsKey(key)){
			versionMap.put(key, 1L);
		}
		return versionMap.get(key);
	}
	
	public static void versionup(String key){
		Long version = findVersionAdd(key);
		versionMap.put(key, ++version);
	}
	
	public static boolean compareToVersion(String key,Long rversion){
		Long version = findVersionAdd(key);
		return rversion.compareTo(version)>=0;
	}
	
	public static SnatchConfig getSnatchConfig() {
		return snatchConfig;
	}

	public static void setSnatchConfig(SnatchConfig snatchConfig) {
		for(SnatchWeb snatchWeb : snatchConfig.getSnatchWebs()){
			for(SnatchGame snatchGame : snatchWeb.getSnatchGames()){
				snatchGame.setSnatchWeb(snatchWeb);
				for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
					snatchItem.setSnatchGame(snatchGame);
					snatchItem.setSnatchWeb(snatchWeb);
				}
			}
		}
		SystemData.snatchConfig = snatchConfig;
	}
	
	
	public static SnatchWeb findSnatchWeb(String webCode){
		List<SnatchWeb> snatchwebs  = snatchConfig.getSnatchWebs();
		for(SnatchWeb snatchWeb : snatchwebs){
			if(snatchWeb.getWebCode().equals(webCode)){
				return snatchWeb;
			}
		}
		return null;
	}
	
	public static SnatchGame findSnatchGame(String webCode,String webGame ){
		SnatchWeb snatchWeb = SystemData.findSnatchWeb(webCode);
		if(null != snatchWeb){
			List<SnatchGame> list = snatchWeb.getSnatchGames();
			for(SnatchGame snatchGame : list){
				if(snatchGame.getGameCode().equals(webGame)){
					return snatchGame;
				}
			}
		}
		return null;
		
	}
	
	public static SnatchItem findSnatchItem(String webCode,String webGame,String itemName,String itmeType){
		SnatchGame snatchGame = findSnatchGame(webCode, webGame);
		if(null == snatchGame) return null;
		for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
			if(itemName.equals(snatchItem.getItemName()) && itmeType.equals(snatchItem.getItemType())){
				return snatchItem;
			}
		}
		return null;
	}
	
	public static List<SnatchGame> findSnatchGames(String webCode ){
		SnatchWeb snatchWeb = SystemData.findSnatchWeb(webCode);
		if(null != snatchWeb){
			return snatchWeb.getSnatchGames();
		}
		return null;
		
	}

	/**
	 * 查询第三方数据
	 * @param webCode
	 * @param webGame
	 * @param gameType
	 * @return
	 */
	public static Map<String, ThirdMatch> findAddThirdMatchs (String webCode,String webGame,String gameType){
		String mapkey = getWebGameMapKey(webCode,webGame);
		ConcurrentHashMap<String, ThirdMatch> thirdMatchMap = findThirdMatchMap(gameType).get(mapkey);
		if(thirdMatchMap==null){
			thirdMatchMap = new ConcurrentHashMap<String, ThirdMatch>();
			findThirdMatchMap(gameType).put(mapkey, thirdMatchMap);
		}
		return thirdMatchMap;
	}
	
	public static void validSpThirdMatch(String webCode,String webGame,String sportinfoid,String matchid){
		ThirdMatch thirdMatch = findThirdMatch(webCode, webGame, SnatchConstant.SNATCH_TYPE_DS, sportinfoid, matchid);
		thirdMatch.setValid(false);
	}
	
	private static Map<String, ConcurrentHashMap<String, ThirdMatch>> findThirdMatchMap(String gameType){
		if(SnatchConstant.SNATCH_TYPE_GQ.equals(gameType)){
			return gqThirdDatas;
		}else{
			return spThirdDatas;
		}
	}
	
	/**
	 * 获取缓存对象
	 * @param webCode
	 * @param webGame
	 * @param gameType
	 * @param sportinfoid
	 * @param matchid
	 * @return
	 */
	public static ThirdMatch findThirdMatch(String webCode,String webGame,String gameType,String sportinfoid,String matchid ){
		String matchkey = getMatchMapKey(sportinfoid, matchid);
		Map<String, ThirdMatch> thirdMatchs = findThirdMatchs(webCode, webGame, gameType);
		return thirdMatchs.get(matchkey);
	}
	
	public static Map<String, ThirdMatch> findThirdMatchs (String webCode,String webGame,String gameType){
		String mapkey = getWebGameMapKey(webCode,webGame);
		return findThirdMatchMap(gameType).get(mapkey);
	}
	
	
	
	public static ThirdLive findLiveData(String webCode,String gameCode,String livekey){
		Map <String, ThirdLive> map = findAddLiveMap(webCode,gameCode);
		return map.get(livekey);
	}
	
	public static void addLiveData(String webCode,String gameCode,String livekey,ThirdLive livedata){
		Map <String, ThirdLive> map = findLiveMap(webCode,gameCode);
		map.put(livekey, livedata);
	}
	
	
	public static Map <String, ThirdLive> findAddLiveMap(String webCode,String gameCode){
		String webmapkey = getWebGameMapKey(webCode,gameCode);
		ConcurrentHashMap<String, ThirdLive>  webmap = liveMatchs.get(webmapkey);
		 if(null == webmap){
			 webmap = new ConcurrentHashMap<String, ThirdLive>();
			 liveMatchs.put(webmapkey, webmap);
		 }
		 return webmap;
	}
	
	public static Map <String, ThirdLive> findLiveMap(String webCode,String gameCode){
		String webmapkey = getWebGameMapKey(webCode,gameCode);
		ConcurrentHashMap<String, ThirdLive>  webmap = liveMatchs.get(webmapkey);
		 return webmap;
	} 
	
	
	
	private static String getWebGameMapKey(String webCode,String webGame){
		return webCode+"_"+webGame;
	}
	
	public static String getMatchMapKey(String sportinfoid,String matchid){
		return sportinfoid+"_"+matchid;
	}
	
	public static String getLiveMatchMapKey(String leagueid,String matchid){
		return leagueid+"_"+matchid;
	}
	
	
	/**
	 * 得到交易项玩法的中文显示时间段
	 * // 1 全场 2上半场 //  11 首盘优胜者  12次盘优胜者 13第三盘优胜者 14第四盘优胜者 15第五盘优胜者 	16局数获胜者
							 //  11 第1节  12第2节  13第3节	 14第4节  				17下半  18加时
	 * @param gametime
	 * @return
	 */
	
//	1 全场 2上半场
	//	1 全场 2上半场      11 第1节  	12第2节  		13第3节	 	14第4节  								3下半  		5加时
	// 	1 全场 2上半场     11 首盘优胜者  	12次盘优胜者 	13第三盘优胜者 	14第四盘优胜者 	15第五盘优胜者 	4局数获胜者
	public static String getTradePlayType(int gametime) {
		switch (gametime) {
		case 1:
			return "全场";
		case 2:
			return "上半场";
		case 3:
			return "下半场";
		case 4:
			return "回合总数";
		case 5:
			return "加时";
		case 11:
			return "第一回合";
		case 12:
			return "第二回合";
		case 13:
			return "第三回合";
		case 14:
			return "第四回合";
		case 15:
			return "第五回合";
		case 16:
			return "第六回合";
		case 17:
			return "第七回合";
		default:
			return "未知";
		}
	}
	
	/**
	 * 返回交易项玩法的中文名称
	 * 
	 * @param tradeItemPlay
	 *            交易项名称
	 * @return
	 */
	public static String getChinesePlayInfo(String tradeItemPlay) {
		String msg = null;
		if (SnatchConstant.STANDARD.equals(tradeItemPlay)) {
			return SnatchConstant.STANDARD_TEXT;
		} else if (SnatchConstant.ALLOW.equals(tradeItemPlay)) {
			return SnatchConstant.ALLOW_TEXT;
		} else if (SnatchConstant.HOSTBIGSMALL.equals(tradeItemPlay)) {
			return SnatchConstant.HOSTBIGSMALL_TEXT;
		} else if (SnatchConstant.AWAYBIGSMALL.equals(tradeItemPlay)) {
			return SnatchConstant.AWAYBIGSMALL_TEXT;
		}else if (SnatchConstant.BIGSMALL.equals(tradeItemPlay)) {
			return SnatchConstant.BIGSMALL_TEXT;
		} else if (SnatchConstant.ONETWO.equals(tradeItemPlay)) {
			return SnatchConstant.ONETWO_TEXT;
		} else if (SnatchConstant.TG.equals(tradeItemPlay)) {
			return SnatchConstant.TG_TEXT;
		} else if (SnatchConstant.DC.equals(tradeItemPlay)) {
			return SnatchConstant.DC_TEXT;
		} else if (SnatchConstant.HTFT.equals(tradeItemPlay)) {
			return SnatchConstant.HTFT_TEXT;
		} else if (SnatchConstant.FTCS.equals(tradeItemPlay)) {
			return SnatchConstant.FTCS_TEXT;
		} else if (SnatchConstant.FIRST.equals(tradeItemPlay)) {
			return SnatchConstant.FIRST_TEXT;
		} 
		return msg;
	}
	
	
	/**
	 * ===============================提供服务的节点=============================================
	 */
	
	/**
	 * 移除任务项
	 * @param onlyCode
	 */
	public static void removeWebSnatchInfo(String onlyCode){
		Iterator<CopyOnWriteArrayList<WebSnatchInfo>> iterator = webSnachInfos.values().iterator();
		while (iterator.hasNext()) {
			List <WebSnatchInfo> list = iterator.next();
			for(int i = 0;i<list.size();i++){
				WebSnatchInfo webSnatchInfo  = list.get(i);
				if(onlyCode.equals(webSnatchInfo.getOnlyCode())){
					list.remove(i);
				}
			}
			
		}
	}
	
	
	/**
	 * 添加任务项
	 * @param onlyCode
	 */
	public static void addWebSnatchInfo(NodeWeb nodeWeb){
		
		List<WebConfig> list = nodeWeb.getWebConfigs();
		for(WebConfig web:list){
			CopyOnWriteArrayList<WebSnatchInfo> webSnatchInfos = webSnachInfos.get(web.getWebCode());
			boolean nohere = true;
			if(webSnatchInfos == null){
				webSnatchInfos = new CopyOnWriteArrayList<WebSnatchInfo>();
				webSnachInfos.put(web.getWebCode(), webSnatchInfos);
			}else{
				for(WebSnatchInfo sebSnatchInfo : webSnatchInfos){
					if(sebSnatchInfo.getOnlyCode().equals(nodeWeb.getOnlyCode())){
						nohere = false;
						break;
					}
				}
			}
			
			if(nohere){
				WebSnatchInfo webinfo = new WebSnatchInfo();
				webinfo.setOnlyCode(nodeWeb.getOnlyCode());
				webinfo.setWebCode(web.getWebCode());
				webinfo.setSumAccount(web.getLoginAccounts().size());
				webinfo.setWebName(web.getWebName());
				webinfo.setUpdateSystemTime(System.currentTimeMillis());
				webSnatchInfos.add(webinfo);
			}
			
		}
		
	}
	
	
	/**
	 * 
	 * @param valid
	 * @param onlyCode
	 * @param webCode  为空时设置所有的状态
	 */
	public static void lostNode(String onlyCode){
		Iterator<CopyOnWriteArrayList<WebSnatchInfo>> iterator = webSnachInfos.values().iterator();
		if(null != iterator){
			while (iterator.hasNext()) {
				List<WebSnatchInfo> list = iterator.next();
				for(WebSnatchInfo webSnatchInfo : list){
					webSnatchInfo.setValid(0);
				}
				
			}
		}
	}
	
	/**
	 * 
	 * @param valid
	 * @param onlyCode
	 * @param webCode  为空时设置所有的状态
	 */
	public static void validNode(int valid,String onlyCode,String webCode,String systemcode,List<String> validlist){
		List<WebSnatchInfo> list = webSnachInfos.get(webCode);
		if(null != list){
			for(WebSnatchInfo webSnatchInfo : list){
				if(webSnatchInfo.getOnlyCode().equals(onlyCode)){
					webSnatchInfo.setSystemcode(systemcode);
					webSnatchInfo.setValid(valid);
					webSnatchInfo.setUpdateSystemTime(System.currentTimeMillis());
					webSnatchInfo.setLoginAccounts(validlist);
				}
			}
		}
				
	}
	
	
	public static List<WebSnatchInfo> getWebSnatchInfos(String webCode){
		return webSnachInfos.get(webCode);
	}
	
	
	//=============================================================
	public static List<GameType> findGameTypes(){
		List<GameType> list = new ArrayList<GameType>();
		GameType gameType1 = new GameType();
		gameType1.setTypeName(SnatchConstant.SNATCH_TYPE_GQ_TEXT);
		gameType1.setTypeCode(SnatchConstant.SNATCH_TYPE_GQ);
		GameType gameType2 = new GameType();
		gameType2.setTypeName(SnatchConstant.SNATCH_TYPE_DS_TEXT);
		gameType2.setTypeCode(SnatchConstant.SNATCH_TYPE_DS);
		GameType gameType3 = new GameType();
		gameType3.setTypeName(SnatchConstant.SNATCH_TYPE_ZC_TEXT);
		gameType3.setTypeCode(SnatchConstant.SNATCH_TYPE_ZC);
		list.add(gameType1);
		list.add(gameType2);
		list.add(gameType3);
		return list;
	}
	
	public static boolean isStandardPlay(String playName){
		if(playName.contains(SnatchConstant.STANDARD)){
			return true;
		}
		if(playName.contains(SnatchConstant.ALLOW)){
			return true;
		}
		if(playName.contains(SnatchConstant.BIGSMALL)){
			return true;
		}
		if(playName.contains(SnatchConstant.ONETWO)){
			return true;
		}
		if(playName.contains(SnatchConstant.HOSTBIGSMALL)){
			return true;
		}
		if(playName.contains(SnatchConstant.AWAYBIGSMALL)){
			return true;
		}
		return false;
	}
	
	
	
	/**
	 * ------------logdb操作-------------
	 */
	public static void putCounter(String key) {
		try {
			if (StringUtils.isBlank(key))
				return;
			if (null == serverCounter.get(key)) {
				serverCounter.put(key, 1L);
			} else {
				serverCounter.put(key, serverCounter.get(key) + 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
}
